package com.hualan.sfzy.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.score.Score;
import com.hualan.sfzy.entity.system.Permission;
import com.hualan.sfzy.entity.system.Role;
import com.hualan.sfzy.entity.system.User;
import com.hualan.sfzy.mapper.score.ScoreMapper;
import com.hualan.sfzy.mapper.system.RolePermissionMapper;
import com.hualan.sfzy.mapper.system.UserMapper;
import com.hualan.sfzy.mapper.system.UserRoleMapper;
import com.hualan.sfzy.service.system.UserService;
import com.hualan.sfzy.utils.MD5Util;
import com.hualan.sfzy.utils.SaltUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author Mindy
 * @Date 2025年07月12日 上午11:13
 * @Version 1.0
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private ScoreMapper scoreMapper;

    @Transactional
    @Override
    public int add(User user) {

        String salt = SaltUtils.generateRandomSalt();
        user.setSalt(salt);
        String start = user.getPassword();
        String end = start + salt;
        String encode = MD5Util.encode(end);
        user.setPassword(encode);
        int num = userMapper.insert(user);

        Integer id = user.getId();
        Score score = new Score();
        score.setUserId(id);
        score.setScore(10.0);
        scoreMapper.insert(score);

        Role role = new Role();
        role.setId(3);
        Integer role_id =role.getId();
        Integer user_id =user.getId();
        Map<String,Object>map =new HashMap<>();
        map.put("role_id",role_id);
        map.put("user_id",user_id);
        System.out.println(map);
        userRoleMapper.insert(map);
        Permission permission = new Permission();
        permission.setId(3);
        Integer permission_id =permission.getId();
        Map<String,Object>maps =new HashMap<>();
        maps.put("role_id",role_id);
        maps.put("permission_id",permission_id);
        rolePermissionMapper.insert(maps);

        return num;
    }

    @Override
    public int remove(Integer id) {
        return userMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        return userMapper.deleteBatch(ids);
    }

    @Override
    public int edit(User user) {
        user.setPassword(MD5Util.encode(user.getPassword()));
        return userMapper.update(user);
    }

    @Override
    public User findById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, User user) {

        PageHelper.startPage(pageNum, pageSize);

        List<User> list = userMapper.select(user);

        PageInfo<User> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();

        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    @Override
    public List<User> find() {
        return userMapper.select(null);
    }


    @Transactional
    @Override
    public User login(User user) {
        String password = user.getPassword();
        String salt= userMapper.selectSalt(user.getUsername());
        String encode = MD5Util.encode(password + salt);
        user.setPassword(encode);
        User login = userMapper.selectByName(user);
        if(login == null){
            throw new RuntimeException("账号或密码错误，请重新输入");
        }
        return login;
    }

    @Override
    public String selectSalt(String username) {
        return userMapper.selectSalt(username);
    }

    @Transactional
    @Override
    public User loginVue(User user) {
        String password = user.getPassword();
        String salt= userMapper.selectSalt(user.getUsername());
        String encode = MD5Util.encode(password + salt);
        user.setPassword(encode);
        User login = userMapper.selectRole(user);
        if(login == null){
            throw new RuntimeException("账号或密码错误，请重新输入");
        }
        return login;
    }

    @Transactional
    @Override
    public int changePwd(Integer id, String oldPwd, String newPwd) {
        User user = userMapper.selectById(id);
        String old = user.getPassword();
        String username = user.getUsername();
        String salt = userMapper.selectSalt(username);
        String oldEncode = MD5Util.encode(oldPwd + salt);
        if (!Objects.equals(old, oldEncode)) {
            throw new RuntimeException("旧密码错误");
        }
        String newEncode = MD5Util.encode(newPwd + salt);
        user.setPassword(newEncode);
        userMapper.updatePwd(user);
        return 1;
    }


}
